<template>
  <div class="zs-login">
    <div class="bg"></div>
    <div class="box">
      <div class="title">智慧园区-登录</div>
      <div class="form">
        <el-form label-position="top" label-width="100px" :model="form" ref="formRef" :rules="rules">
          <el-form-item label="账号" prop="username">
            <el-input placeholder="请输入账号" v-model="form.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="form.password" show-password type="password" placeholder="请输入密码" ></el-input>
          </el-form-item>
          <el-form-item prop="isShow">
            <el-checkbox v-model="form.isShow">记住我</el-checkbox>
          </el-form-item>
          <el-button class="button" type="primary" @click="register" >登录</el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script setup>
import { reactive, ref } from 'vue'
import {login} from "../api/user"
import store from "../stores/counter.js"
import router from '../router';
  const counter = store()
  const formRef = ref(null)
  const form = ref({username:'demo',password:'zh@hm#23',isShow:false})
  const rules = reactive({
    username:[
      {required:true,message:'请输入账号',trigger:'blur'}
    ],
    password:[
      {required:true,message:'请输入密码',trigger:'blur'},
      {min:5,max:16,message:'请输入正确的手机格式',trigger:'blur'}
    ],
    isShow:[
      {trigger:'blur',validator:(rule,value,callback)=>{
        form.value.isShow?callback():callback(new Error('请记住我'))
       
      }}
    ]
  })
  const register = ()=>{
    formRef.value.validate(async valid=>{
      if(valid){
        const {data} = await login({username:form.value.username,password:form.value.password})
        counter.token = data.token
        router.push('/')
      }
    })
  }
</script>

<style scoped>
.zs-login{
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #fff;
}
.bg{
  width: 60vw;
  height: 100vh;
  border-top-right-radius: 42px;
  border-bottom-right-radius: 42px;
  background: url("../assets/login-bg-e4ea539c.svg");
}
.box{
  display: flex;
  flex-direction: column;
  margin: auto;
}
.title{
    padding-bottom: 76px;
    font-size: 26px;
    font-weight: 500;
    color: #1e2023;
}
/* .el-input{
  width: 300px;
} */
.el-form-item{
  margin-bottom: 42px;
}
.button{
  width: 300px;
  border-color: #4770ff;
  background: #4770FF;
}
</style>
